package com.huiminpay.merchant.controller;

import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.PhoneUtil;
import com.huiminpay.merchant.api.MerchantService;
import com.huiminpay.merchant.common.intercept.util.SecurityUtil;
import com.huiminpay.merchant.convert.MerchantDetailConvert;
import com.huiminpay.merchant.convert.MerchantDetailRegisterConvert;
import com.huiminpay.merchant.dto.MerchantDto;
import com.huiminpay.merchant.service.FileService;
import com.huiminpay.merchant.service.SmsService;

import com.huiminpay.merchant.vo.MerchantDetailVO;
import com.huiminpay.merchant.vo.MerchantRegisterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Api(tags = "商户服务")
@RestController
public class MerchantController {
    @Autowired
    public SmsService smsService;
    @Reference
    private MerchantService merchantService;
    @Autowired
    private FileService fileService;

    @GetMapping("/merchants/{id}")
    @ApiOperation("根据商户id查询商户信息")
    @ApiImplicitParam(name="id",value ="商户主键" ,required = true,dataType = "Long",paramType = "path")
    public MerchantDto queryMerchantById(@PathVariable("id") Long id){
        MerchantDto merchantDTO = merchantService.queryMerchantById(id);
        return merchantDTO;
    }
    @ApiOperation("获取登陆用户的商户信息")
    @GetMapping("/my/merchants")
    public MerchantDto getMerchantInfo(){
        Long merchantId = SecurityUtil.getMerchantId();
        return merchantService.queryMerchantById(merchantId);
    }

    @GetMapping("/sms")
    @ApiOperation("发送验证码")
    @ApiImplicitParam(name="phone",value = "手机号",dataType = "String",paramType = "query")
    public String getCmsCode( String phone){
       log.info("向手机号:{}发送验证码",phone);
        return smsService.sendSms(phone);
    }
    @ApiOperation("注册商户")
    @ApiImplicitParam(name="registerVo",value = "注册信息",dataType = "MerchantRegisterVo",paramType = "body")
    @PostMapping("/merchants/register")
    public MerchantRegisterVo merchantRegisterVo(@RequestBody MerchantRegisterVo registerVo){
        if (registerVo==null){
            throw new BusinessException(CommonErrorCode.E_100108); }
        //手机号为空
        if (StringUtils.isEmpty(registerVo.getMobile())){
            throw new BusinessException(CommonErrorCode.E_100112);
        }
        if (!PhoneUtil.isMatches(registerVo.getMobile())){
            throw new BusinessException(CommonErrorCode.E_100109); }
        if (StringUtils.isBlank(registerVo.getUsername())){
            throw new BusinessException(CommonErrorCode.E_100110); }
        if (StringUtils.isBlank(registerVo.getPassword())){
            throw new BusinessException(CommonErrorCode.E_100111); }
        if (StringUtils.isBlank(registerVo.getVerifiyCode())
                ||StringUtils.isBlank(registerVo.getVerifiykey())){
            throw new BusinessException(CommonErrorCode.E_100113); }
     //调验证码服务校验验证码
       smsService.verifyCode(registerVo.getVerifiykey(),registerVo.getVerifiyCode());
        //掉商户服务新增商户
        MerchantDto merchantDto = MerchantDetailRegisterConvert.INSTANCE.merchantVo2Dto(registerVo);
//        MerchantDto merchantDto = new MerchantDto();
//        BeanUtils.copyProperties(registerVo,merchantDto);
        merchantService.createMerchant(merchantDto);
        return MerchantDetailRegisterConvert.INSTANCE.merchantDto2Vo(merchantDto);
        //return registerVo;
    }

    @ApiOperation("上传证件照")
    @PostMapping("/upload")
    public String upload(@ApiParam(value = "证件照",required = true)
                             @RequestParam("file")  MultipartFile file)
            throws BusinessException {
      return fileService.upload(file);
    }
    @ApiOperation("资质申请")
    @PostMapping("/my/merchants/save")
    @ApiImplicitParam(name = "merchantDetailVO",value = "商户认证资料",required = true,
            dataType = "MerchantDetailVO",paramType = "body")
    public void saveMerchant(@RequestBody MerchantDetailVO merchantDetailVO) {
        Long merchantId = SecurityUtil.getMerchantId();
        //Long merchantId = 1868485836400521218L;
        MerchantDto merchantDto = MerchantDetailConvert.INSTANCE.Vo2Dto(merchantDetailVO);
        merchantService.applyMerchant(merchantId,merchantDto);
    }

}
